/**
 * Radio Story Cards - using RFID tags to track physical story cards in an agile/lean environment.
 *
 * See http://www.radiostorycards.com for more information.
 *
 *
 * Copyright (C) 2011, Ted M. Young
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.radiostorycards.domain.cards;

import org.joda.time.DateTime;

import java.util.Arrays;
import java.util.List;

/**
 * The state of a card is the column in which it's in, and the date-time that it entered that state.
 *
 * @author Ted Young <tedyoung@gmail.com> @JitterTed
 */
public class CardState {
  private final Column _column;
  private final DateTime _dateTime;

  private final List<Column> COLUMN_ORDER = Arrays.asList(Column.values());

  public CardState(DateTime dateTime) {
    this(Column.Backlog, dateTime);
  }

  public CardState(Column column, DateTime dateTime) {
    _dateTime = dateTime;
    _column = column;
  }

  public Column getColumn() {
    return _column;
  }

  public DateTime getDateTime() {
    return _dateTime;
  }

  public CardState transitionToNextState(DateTime dateTime) {
    return new CardState(getNextColumn(), dateTime);
  }

  public Column getNextColumn() {
    return COLUMN_ORDER.get(COLUMN_ORDER.indexOf(_column) + 1);
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    CardState cardState = (CardState) o;

    return _column == cardState._column && !(_dateTime != null ? !_dateTime.equals(cardState._dateTime) : cardState._dateTime != null);

  }

  @Override
  public int hashCode() {
    int result = _column != null ? _column.hashCode() : 0;
    result = 31 * result + (_dateTime != null ? _dateTime.hashCode() : 0);
    return result;
  }

  @Override
  public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append("CardState");
    sb.append("{_column=").append(_column);
    sb.append(", _dateTime=").append(_dateTime);
    sb.append('}');
    return sb.toString();
  }

}
